package com.botarena.client.battle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.botarena.client.SiteTokens;
import com.botarena.client.UIHelper;
import com.botarena.client.battle.remote.BattleAccess;
import com.botarena.client.battle.remote.BattleAccessAsync;
import com.botarena.client.ui.StringHelper;
import com.botarena.shared.BattleInfo;
import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.Range;
import com.google.gwt.view.client.RangeChangeEvent;

/**
 * Wyswietla ostatnie walki
 * 
 * @author Aleksandra Lipiec
 * 
 */
public class BattlesPanel extends VerticalPanel {
	
	public static final List<BattleInfo> BATTLE_SAMPLE = Arrays
			.asList(new BattleInfo("OIAWEJDWIOoiawiwuaiw", "Zdzisio", "Zdenek",
					1, "Main", "9.05.2011 22:30"));
	
	public static final int PAGE_SIZE = 10;

	private CellTable<BattleInfo> battleTable;
	
	List<BattleInfo> data;
	
	public BattlesPanel() {
		setWidth("100%");
		battleTable = new CellTable<BattleInfo>();
		battleTable.setWidth("100%");

		buildColumnModel(battleTable);
		
		this.add(battleTable);
	}
	
	public void setData(List<BattleInfo> data){
		this.data = data;
		battleTable.setRowCount(data.size());
		battleTable.setRowData(data);
	}
	
	public void setPager(SimplePager pager){
		pager.setDisplay(battleTable);
	}
	
	public void addRangeHandler() {
		battleTable.addRangeChangeHandler(new RangeChangeEvent.Handler() {

			@Override
			public void onRangeChange(RangeChangeEvent event) {
				Range range = battleTable.getVisibleRange();
				final int start = range.getStart();
				int length = range.getLength();
				final List<BattleInfo> res = new ArrayList<BattleInfo>(length);
				List<String> keys = new ArrayList<String>(length);
				for (int i = start; i < start + length && i < data.size() + 2; i++)
					keys.add(data.get(i).getBattleKey());
				BattleAccessAsync daa = GWT.create(BattleAccess.class);
				daa.getBattlesPart(keys, new AsyncCallback<List<BattleInfo>>() {

					@Override
					public void onSuccess(List<BattleInfo> result) {
						for (BattleInfo battle : result) {							
							res.add(battle);
						}

						battleTable.setRowData(start, res);
					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO ?
						caught.printStackTrace();
					}
				});

			}
		});
	}

	private void buildColumnModel(CellTable<BattleInfo> battleTable) {
		TextColumn<BattleInfo> dateCol = new TextColumn<BattleInfo>() {
			@Override
			public String getValue(BattleInfo pos) {
				return pos.getDate();
			}
		};
		battleTable.addColumn(dateCol, "DATE");

		TextColumn<BattleInfo> bot1Col = new TextColumn<BattleInfo>() {
			@Override
			public String getValue(BattleInfo pos) {
				return StringHelper.limitedLengthString(pos.getBot1Name(), UIHelper.MAX_NAME_LEN);
			}
		};
		battleTable.addColumn(bot1Col, "BOT1");

		TextColumn<BattleInfo> bot2Col = new TextColumn<BattleInfo>() {
			@Override
			public String getValue(BattleInfo pos) {
				return StringHelper.limitedLengthString(pos.getBot2Name(), UIHelper.MAX_NAME_LEN);
			}
		};
		battleTable.addColumn(bot2Col, "BOT2");

		TextColumn<BattleInfo> winnerCol = new TextColumn<BattleInfo>() {
			@Override
			public String getValue(BattleInfo pos) {
				if (pos.getWinner() == 1)
					return "BOT1";
				if (pos.getWinner() == 2)
					return "BOT2";
				return "-";
			}
		};
		battleTable.addColumn(winnerCol, "WINNER");

		Column<BattleInfo, String> preview = new Column<BattleInfo, String>(
				new ButtonCell()) {

			@Override
			public String getValue(BattleInfo info) {
				return "Preview";
			}

		};
		preview.setFieldUpdater(new FieldUpdater<BattleInfo, String>() {

			@Override
			public void update(int index, BattleInfo battleInfo, String value) {
				StringBuffer buf = new StringBuffer("http://");
				buf.append(Location.getHost()).append("#").append(
						SiteTokens.BATTLE_PREV).append('?').append(
						battleInfo.getBattleKey());
				Location.replace(buf.toString());
			}
		});

		battleTable.addColumn(preview, "PREVIEW");
	}

}
